<?php
/**
 * 分销
 */
namespace Qgs\Controller;


class DistributionController extends BaseController
{
    public $_local_id;
    public function __construct()
    {
        parent::__construct();
        $this->_local_id = session('userInfo')['local_id'];
    }

    /**
     * 奖金配置
     */
    public function setMoney()
    {
        $local_store = M('webpos_local_store')->field('is_open_distribution')->where(['local_id' =>$this->_local_id])->find();

        $this->assign([
            'is_open_distribution' => $local_store['is_open_distribution'],
        ]);
        $store_distribution_set = M('store_distribution_set');

        if (IS_POST){
            $follow_wechat = I('follow_wechat');
            $generate_poster = I('generate_poster');
            $son_shopping = I('son_shopping');

            $store_distribution_set->query("REPLACE INTO sdb_store_distribution_set (local_id,set_name,set_value) VALUES($this->_local_id, 'follow_wechat', '$follow_wechat')");
            $store_distribution_set->query("REPLACE INTO sdb_store_distribution_set (local_id,set_name,set_value) VALUES($this->_local_id, 'generate_poster', '$generate_poster')");
            $store_distribution_set->query("REPLACE INTO sdb_store_distribution_set (local_id,set_name,set_value) VALUES($this->_local_id, 'son_shopping', '$son_shopping')");
        }

        $follow_wechat = $store_distribution_set->where(['local_id' => $this->_local_id, 'set_name' => 'follow_wechat'])->find();
        $generate_poster = $store_distribution_set->where(['local_id' => $this->_local_id, 'set_name' => 'generate_poster'])->find();
        $son_shopping = $store_distribution_set->where(['local_id' => $this->_local_id, 'set_name' => 'son_shopping'])->find();

        $this->assign([
            'follow_wechat' => $follow_wechat['set_value'],
            'generate_poster' => $generate_poster['set_value'],
            'son_shopping' => $son_shopping['set_value'],
        ]);

        $this->display();

    }

    /**
     * 是否开启分销
     */
    public function isOpen(){
        $v = I('state');
        if ($v == 'true') {
            $v = '1';
        }else{
            $v = '0';
        }
        $local_store = M('webpos_local_store')->where(['local_id' =>$this->_local_id])->save(['is_open_distribution' => $v]);
        if ($local_store) {
            echo 1;
        }else{
            echo 0;
        }
    }

    /**
     * 分销人员列表
     */
    public function memberList(){

        $select = I('select');
        $value = I('value');

        if (!empty($select)) {
            $where['b.'.$select] = ['like',"%$value%"];
        }

        $where['a.local_id'] = $this->_local_id;
        $order = 'a.id desc';
        $field = 'a.*,b.nickname,b.mobile,b.name';
        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $count = M('distribution_member a')
            ->join("sdb_b2c_members b ON a.member_id = b.member_id", 'left')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $distribution_member = M('distribution_member a')
            ->join("sdb_b2c_members b ON a.member_id = b.member_id", 'left')
             ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();

        $this->assign([
            'msg' => $distribution_member,
            'show' => $show
        ]);

        $this->display();
    }

    /**
     * 奖金记录
     */
    public function bonusRecord(){

        $type = I('type');
        $where['local_id'] = $this->_local_id;
        $where['member_id'] = I("member_id");
        if (!empty($type) || $type == '0') {
            $where['type'] = "$type";
        }
        $order = 'id desc';
        $field = '*';
        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $count = M('b2c_bonus_log')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $msg = M('b2c_bonus_log')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();

        $this->assign([
            'msg' => $msg,
            'show' => $show
        ]);
        $this->display();
    }



}